<template>
  <!--投资日常-->
    <div>
        <public-search
                :fields="fields"
                :search-param="form"
                @on-query="onQuery"
                @on-reset="onReset"
        />
        <public-table
                index
                :page="page"
                :tableData="tableData"
                :tableOption.sync="tableOption"
                @page-change="onPageChange"
        >
            <template #buttons="scope">
                <el-button
                        class="button_funtion"
                        size="large"
                        @click="toPage('dataFilling',scope.row)"
                >资料填报</el-button>
                <el-button
                        class="button_funtion"
                        size="large"
                        @click="toPage('meetingMinutes',scope.row)"
                >会议记录</el-button>
                <el-button
                        class="button_funtion"
                        size="large"
                        @click="toPage('incomeStatement',scope.row)"
                >全年经营预算</el-button>
                <el-button
                        class="button_funtion"
                        size="large"
                        @click="toPage('conditionOfBusiness',scope.row)"
                >经营情况</el-button>
            </template>
        </public-table>
    </div>
</template>

<script setup>
import PublicTable from "@/components/PublicTable.vue";
import {onMounted, reactive, ref} from "vue";
import {tableCommonFun} from "@/hooks/tableCommon";
import PublicSearch from "@/components/PublicSearch.vue";
import {otherApi} from "@/api/investment";
import {department} from "@/api/project";
import {useRouter} from "vue-router";

const interfaceConfig = reactive({
    fileName:'investment',
    apiPath:'investmentDailyApi.getDailyList',
});
const exportConfig = reactive({
    fileName:'businessFinance',
    apiPath:'culturalCreativityApi.exportCultural',
    tableName:'文创合同表'
});

const {
    form,
    page,
    tableOption,
    tableData,
    onQuery,
    onReset,
    onPageChange,
    getData,
    onExport
}  = tableCommonFun(interfaceConfig,exportConfig);

form.value = {
    startTime:'',
    endTime:'',
    projectTypeId:'',
    companyId:'',
    responsibleUserId:'',
    keyword:''
}

tableOption.value = [
    { label: "项目名称 ", prop: "projectName", minWidth: 100 },
    { label: "项目类型", prop: "projectTypeName", minWidth: 100 },
    { label: "实施主体", prop: "companyName", minWidth: 100 },
    { label: "行业类型", prop: "industryTypeName", minWidth: 100 },
    {
        label: "项目性质", prop: "projectNature", formatter: (val) => {
            return ['无', '新建项目', '续建项目', '新设立','续投'][val];
        }, minWidth: 100
    },
    { label: "发起人", prop: "responsibleUserName", minWidth: 100 },
    { label: "拟计划总投额(投资规模)/万", prop: "projectAmount",formatter: (val) => {
            if(val && typeof val === 'number'){
                return (val/10000).toFixed(2);
            }
            return '-';
        }
    },
    {
        label: '操作',
        prop: 'buttons',
        slot: true,
        width: 450,
        overHidden: false,
        fixed: "right"
    }
];

const fields = ref([
    {   type:'input',
        label:'项目名称',
        key:'keyword',
        placeholder:'请输入项目名称',
        clearable:true
    },
    {   type:'datetimerange',
        label:'创建时间',
        key:'valueTime',
        startTimeName:'startTime',
        endTimeName:'endTime',
        placeholder:'请选择创建时间',
        clearable:true,
        fun:(e,params)=>{
            if(e && e instanceof Array && e.length>0){
                form.value[params.startTimeName] = e[0];
                form.value[params.endTimeName] = e[1];
            }
            if(!e || (e instanceof  Array && e.length===0)){
                form.value[params.startTimeName] = '';
                form.value[params.endTimeName] = '';
            }
        }
    },
    {   type:'select',
        label:'项目类型',
        key:'projectTypeId',
        placeholder:'请选择项目类型',
        clearable:true,
        options:{
            labelKey:'enumName',
            valueKey:'enumId',
            list:[]
        }
    },
    {   type:'select',
        label:'项目部门',
        key:'departmentId',
        placeholder:'请选择项目部门',
        clearable:true,
        options:{
            labelKey:'departmentName',
            valueKey:'departmentId',
            list:[]
        },
        fun:(e)=>{
            getLeaderByDepartmentId(e);
        }
    },
    {
        type:'select',
        label:'发起人',
        key:'responsibleUserId',
        placeholder:'请选择发起人',
        clearable:true,
        options:{
            labelKey:'userName',
            valueKey:'userId',
            list:[]
        }
    },
]);

/**
 * 项目类型
 */
const getListEnum = (type) => {
    otherApi.getListEnum({type: type}).then(res=>{
        const {code,data} = res;
        if(code == 200){
            fields.value[2].options.list = data;
        }
    });
}

/**
 * 获取所有部门
 */
const getAllDepartments = () => {
    department.getAllDepartments().then((res) => {
        const { code, data, msg } = res;
        if (code == 200) {
            fields.value[3].options.list = data;
        }
    });
};

/**
 * 根据部门id查负责人
 * @param responsibleDepartmentId
 */
const getLeaderByDepartmentId = (responsibleDepartmentId) => {
    department.getLeaderByDepartmentId(responsibleDepartmentId).then((res) => {
        const { code, data, msg } = res;
        if (code === '200') {
            fields.value[4].options.list = data;
        }
    });
};

const router = useRouter();
const toPage = (type,row) => {
    switch (type) {
        case 'incomeStatement': // 利润表/ 全年经营预算
            // router.push({path:`/investmentProjectManagement/investmentProjects/investmentProjectInformation/viewCostEstimates/${row.projectId}/${row.projectName}`});
            router.push({path:`/investmentProjectManagement/investmentDaily/annualOperatingBudget/projectId=${row.projectId}`});
            break;
        case 'dataFilling': // 资料填报
            router.push({path:`/investmentProjectManagement/investmentDaily/dataFilling/${row.projectId}`});
            break;
        case 'meetingMinutes': // 会议记录
            router.push({path:`/investmentProjectManagement/investmentDaily/meetingMinutes/${row.projectId}`});
            break;
        case 'conditionOfBusiness': // 经营情况
            router.push({path:`/investmentProjectManagement/investmentDaily/conditionOfBusiness/${row.projectId}`});
            break;
    }
}

onMounted(()=>{
    getListEnum(1);
    getAllDepartments();
    getData({
        pageNum:1,
        pageSize:10,
        param:{}
    });
});
</script>

<style scoped>

</style>
